package com.ship.control;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.validation.BindException;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractCommandController;

import com.ship.common.SessionInfo;
import com.ship.enume.UserType;
import com.ship.enume.WenumTypeEditor;


/**
 * 系统后台管理命令控制�?
 * @author wjh
 * @version  Nov 14, 2011 2:12:08 PM
 */
public abstract class AbstractAdminCommandController extends AbstractCommandController {

	//只是为满足AbstractCommandController的要�?
	public AbstractAdminCommandController(){
		this.setCommandClass(String.class);
	}
	
	/**
	 * 子类必须通过此方法获取AdminSessionInfo对象
	 * @param request
	 * @return
	 */
	public SessionInfo getSessionInfo(HttpServletRequest request){
		return authorizationCredit.getSessionInfo(request);
	}
	
	/**
	 * 控制器请求入口，内有登录判断
	 */
	protected final ModelAndView handle(HttpServletRequest request,HttpServletResponse response,
			Object command,BindException errors) throws Exception {
		
		if(!authorizationCredit.signinValidate(request)){
			authorizationCredit.redirectSigninValidateFailView(request, response);
			return null;
		}
		else{
			ModelAndView modelAndView = executeRequest(request,response);			
			if(modelAndView == null){
				modelAndView = executeRequest(request,response,command);
			}
			
			return modelAndView;
		}
		
	}
	/**
	 * 内置处理
	 * @author wzq
	 */
	protected final void initBinder(HttpServletRequest request, ServletRequestDataBinder binder)
    throws Exception {
		//日期处理
		DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
		CustomDateEditor dateEditor = new CustomDateEditor(fmt, true);
		binder.registerCustomEditor(Date.class, dateEditor);
		binder.registerCustomEditor(UserType.class, null, new WenumTypeEditor(UserType.class));

	}
	
	/**
	 * 不含命令对象的请求处理方法�?
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	protected ModelAndView executeRequest(HttpServletRequest request,HttpServletResponse response)
		throws Exception{
		return null;
	}
	
	/**
	 * 含命令对象的请求处理方法�?
	 * @param request
	 * @param response
	 * @param command
	 * @return
	 * @throws Exception
	 */
	protected ModelAndView executeRequest(HttpServletRequest request,HttpServletResponse response,Object command)
		throws Exception{
		return null;
	}

	/**
	 * 授权信任处理对象
	 */
	private AuthorizationCredit authorizationCredit;
	private String view;
	private String selView;
	
	public void setAuthorizationCredit(AuthorizationCredit authorizationCredit) {
		this.authorizationCredit = authorizationCredit;
	}
	
	public String getView(HttpServletRequest request){
		SessionInfo sessionInfo=this.getSessionInfo(request);
		return sessionInfo.getUserType()==UserType.admin?this.view:this.selView;
	}
	
	public void setView(String view) {
		this.view = view;
	}
	
	public void setSelView(String selView) {
		this.selView = selView;
	}
	
	
}
